$(function(){
    "use strict";
    // 当点击直接投入的时候
    $('.directInto_box>p').click(function(){
        $(this).siblings('ul.directInto_list').toggleClass('active')
    })
    // 点击出现搜索框
    $('.hidden_box_showBox').click(function(){
        $(this).siblings('.hidden_box_hiddenBox').toggleClass('active')
        if($(this).children('i').hasClass('icon-jiantou')){
            $(this).children('i').removeClass('icon-jiantou').addClass('icon-jiantou-top')
        }else{
            $(this).children('i').removeClass('icon-jiantou-top').addClass('icon-jiantou')
        }
    })
    // 当确定搜索时
    $('.hiddenBox_list_btn').on('click','button',function(){
        $('.hidden_box_hiddenBox').removeClass('active')
        $('.hidden_box_showBox').children('i').removeClass('icon-jiantou-top').addClass('icon-jiantou')
        if($(this).html() == '确定'){
            $('#dateVal').html(window.query_year)
        }else{
            window.query_year = null
        }
    })
    //当点击查看记录时
    $('#table_table').on('click','.look_record_btn',function(e){
        e.preventDefault()
        $(this).siblings('.tip_hiddenBox').addClass('active')
    })
    // 当点击工时记录中确认按钮时
    $('#table_table').on('click','.work_record_box>.btn',function(e){
        e.preventDefault()
        $(this).parent().removeClass('active')
    })
    //全选的功能
    $('#checkAll').on('click',function(){
        var checkboxs =$('input[type=checkbox]:not("#checkAll")');
        for(var i=0;i<checkboxs.length;i++){
            var checkbox=checkboxs[i];
            checkbox.checked=this.checked;
        }
    })
    $('#table_table').on('click','input[type=checkbox]:not("#checkAll")',function(){
        "use strict";
        var isCheckedAll = true;
        var checkboxs =$('input[type=checkbox]:not("#checkAll")');
        for (var i = 0; i < checkboxs.length; i++) {
            if (!checkboxs[i].checked) {
                isCheckedAll = false;
                break;
            }
        }
        $('#checkAll')[0].checked = isCheckedAll;
    })

    /*当点击添加按钮时*/
    $('.operation_btn').on('click','a',function(e){
        e.preventDefault()
        if( $(this).html() == '添加' ){
            var add_people = $(this).attr('href')
            $(`[lll=${add_people}]`).addClass('active')
        }
        if( $(this).html() == '核算' ){
            var add_people = $(this).attr('href')
            $(add_people).addClass('active')
        }
        if( $(this).html() == '批量删除' ){
            var checkboxs =$('input[type=checkbox]:not("#checkAll")');
            var len = 0;
            for (var i = 0; i < checkboxs.length; i++) {
                if (checkboxs[i].checked) {
                    continue
                }else{
                    len++
                }
            }
            if(len == checkboxs.length){
                $('#yz_modal').html('您还没有选择人员！')
                $('#yz_modal').addClass('active')
                tipShow()
                return
            }
            let _delete = $(this).attr('href');
            $(_delete).addClass('active')
        }
    })
    // 批量删除
    $('#delete .delete_tip_btn_box').on('click','button',function(){
        "use strict";
        let delete_arr = [];
        if($(this).html() == '确认'){
            let checkboxs =$('#table_table input[type=checkbox]:not("#checkAll")');
            for (let i = 0; i < checkboxs.length; i++) {
                if (checkboxs[i].checked) {
                    delete_arr.push( $(checkboxs[i]).closest('tr[data-index]').attr('data-index') )
                }else{
                    continue;
                }
            }
            $.ajax({
                url:'http://jk.com/Admin/CostStaff/multiDelCostStaff',
                type:'post',
                data:{
                    cost_id: delete_arr
                },
                success:function(data){
                    if(data.code==1){
                        for(let r = 0; r < delete_arr.length; r++){
                            $('#table_table').children('tbody').children(`[data-index=${delete_arr[r]}]`).remove();
                        }
                        $('#delete').removeClass('active')
                    }else{
                        $('#yz_modal').html(data.msg)
                        $('#yz_modal').addClass('active')
                        tipShow()
                        return
                    }
                    checkBox()
                }
            })
        }else{
            $('#delete').removeClass('active')
        }

    })
    // 当点击编辑按钮时
    $('#table_table').on('click','.edit_table',function(e){
        e.preventDefault()
        let edit_box = $(this).attr('href')
        let edit_id = $(this).closest('tr').attr('data-index')

        if($(this).html()=='编辑'){
            $.ajax({
                url:'http://jk.com/Admin/CostStaff/editCostStaff',
                type:'get',
                data:{
                    id:edit_id
                },
                success:function(data){
                    if(data.code==1){
                        let datas = data.data
                        let add_voucher = (datas.vouch_sn).split('-')
                        $(`[lll=${edit_box}]`).find('[add_name=edit_voucher_type]').val(add_voucher[0])
                        $(`[lll=${edit_box}]`).find('[add_name=edit_voucher_num]').val(add_voucher[1])
                        $(`[lll=${edit_box}]`).find('[add_name=edit_time]').val(datas.cost_time)
                        $(`[lll=${edit_box}]`).find('[add_name=edit_borrowingChoose]').val(datas.cost_type)
                        $(`[lll=${edit_box}]`).find('[add_name=edit_happenMoney]').val(datas.cost_fmoney)
                        $(`[lll=${edit_box}]`).find('[add_name=edit_abstract]').val(datas.cost_desc)

                        let all_cost = data.all_cost
                        let costHTML = ''
                        for(let i=0;i<all_cost.length;i++){
                            costHTML += `
                            <option value="${all_cost[i].money}" data-id="${all_cost[i].id}">${all_cost[i].type}</option>
                        `
                        }
                        $(`[lll=${edit_box}]`).find('[add_name=edit_Relation_money]').html(costHTML)

                        /**/
                        // console.log(datas.cost_tmoney)
                        // console.log($(`[lll=${edit_box}]`).find('[add_name=edit_Relation_money]'))
                        // console.log(datas.time)
                        $(`[lll=${edit_box}]`).find('[add_name=edit_Relation_time]').val(datas.time)
                        $(`[lll=${edit_box}]`).find('[add_name=edit_Relation_money]').val(datas.cost_tmoney)
                        // console.log($(`[lll=${edit_box}]`).find('[add_name=edit_Relation_money]').val())
                        $(`[lll=${edit_box}]`).find('[add_name=edit_Relation_sum]').val(datas.cost_tmoney)
                        /**/
                        let fileBtn = $(`[lll=${edit_box}]`).find('[add_name="edit_file"]');
                        let imgs = datas.imgs;
                        let imgHTML = '';
                        let imgEditArr2 = []
                        for(var imgi=0;imgi<imgs.length;imgi++){
                            imgHTML += `
                                <li class="pz_file_list_text">
                                    <span>${imgs[imgi].fname} ${imgs[imgi].fsize}kb</span>
                                    <i class="iconfont icon-cha"></i>
                                </li>
                        `
                            let imgEditArr = []
                            imgEditArr.push(imgs[imgi].base64)
                            imgEditArr.push(imgs[imgi].fname)
                            imgEditArr.push(imgs[imgi].fsize)
                            imgEditArr2.push(imgEditArr)
                        }
                        $(fileBtn).parent().siblings('.pz_file_list').html(imgHTML);

                        $(`[lll=${edit_box}]`).addClass('active')
                        window.sessionStorage['edit_id']=edit_id
                        let edit_img_box = {
                            imgEditArr2:imgEditArr2
                        }

                        window.sessionStorage['edit_img_box'] = JSON.stringify(edit_img_box)

                    }else{

                    }
                }
            })
        }else{
            $.ajax({
                url:'http://jk.com/Admin/CostStaff/editCostStaff',
                type:'get',
                data:{
                    id:edit_id
                },
                success:function(data){
                    if(data.code==1){
                        let datas = data.data;
                        let showimgHTML = ''
                        for(var showimgi=0;showimgi<datas.imgs.length;showimgi++){
                            showimgHTML += `
                                <li><a data-magnify="gallery" href="${datas.imgs[showimgi].img}"><img src="${datas.imgs[showimgi].thumb}"></a></li>
                            `
                        }
                        $('#look_img').children('.look_img_list').html(showimgHTML)

                    }else{

                    }
                }
            })
            $(`[lll=${edit_box}]`).addClass('active')
        }

    })
    //当凭证查看点击确定的时候
    $('#look_img').on('click','.btn-success',function(){
        $('#look_img').removeClass('active')
    })
    //上传凭证
    let fileArr = [];
    let IMG_arr2 = [];
    $('.hidden_input').on('change',function(){
        // console.log(window.sessionStorage['edit_id'])
        // let oldedit_id = window.sessionStorage['edit_id']
        let data = this.files;
        for(var i =0;i<data.length;i++){
            let fileReader = new FileReader();
            fileReader.readAsDataURL(data[i]);
            let size = Math.round(data[i].size/1024);
            let name = data[i].name;
            let pz_file_listHTML = $(this).parent().siblings('.pz_file_list').html();
            pz_file_listHTML += `<li class="pz_file_list_text">
                                    <span>${name} ${size}kb</span>
                                    <i class="iconfont icon-cha"></i>
                                </li>
                                 `
            $(this).parent().siblings('.pz_file_list').html(pz_file_listHTML);
            fileReader.onload = function (e){
                let url = this.result;//上传文件的URL
                // let img_btnSrc = url.indexOf('4');
                // let img = url.substring(img_btnSrc + 2);
                let IMG_arr = [];
                IMG_arr.push(url);
                IMG_arr.push(name);
                IMG_arr.push(size);

                IMG_arr2.push(IMG_arr)
            }

        }
    })
    //当删除某个凭证的时候
    $('.pz_file_list').on('click','i',function(e){
        e.preventDefault();
        fileArr.splice($(this).parent().index(),1);
        // IMG_arr.splice($(this).parent().index(),1);
        IMG_arr2.splice($(this).parent().index(),1);
        let edit_img_box_old = JSON.parse(window.sessionStorage['edit_img_box']).imgEditArr2
        edit_img_box_old.splice($(this).parent().index(),1);
        let edit_img_box = {
            imgEditArr2:edit_img_box_old
        }
        window.sessionStorage['edit_img_box'] = JSON.stringify(edit_img_box)
        $(this).parent().remove()
    })

    /*燃料金额 自动计算*/
    // $('[add_name=add_num]').blur(function(){
    //     let add_num = $(this).val();
    //     let add_price = $(this).closest('[lll]').find('[add_name=add_price]').val();
    //     if(add_num&&add_price){
    //         let add_sum_money = Number(add_num)*Number(add_price)
    //         $(this).closest('[lll]').find('[add_name=add_sum_money]').val(add_sum_money.toFixed(2))
    //     }else{
    //         $(this).closest('[lll]').find('[add_name=add_sum_money]').val('')
    //         return
    //     }
    // })
    // $('[add_name=add_price]').blur(function(){
    //     let add_price = $(this).val();
    //     let add_num = $(this).closest('[lll]').find('[add_name=add_num]').val();
    //     if(add_price&&add_num){
    //         let add_sum_money = Number(add_num)*Number(add_price)
    //         $(this).closest('[lll]').find('[add_name=add_sum_money]').val(add_sum_money.toFixed(2))
    //     }else{
    //         $(this).closest('[lll]').find('[add_name=add_sum_money]').val('')
    //         return
    //     }
    // })
    /*关联费用选择之后自动计算金额*/
    $('[add_name=add_Relation_money]').change(function(){
        let add_Relation_money = $(this).val()
        $(this).closest('[lll]').find('[add_name=add_Relation_sum]').val(add_Relation_money);
    })
    // 确认按钮点击时
    $('#add_btn').click(function(){
        let add_voucher_type = $(this).closest('[lll]').find('[add_name=add_voucher_type]').val()
        let add_voucher_num = $(this).closest('[lll]').find('[add_name=add_voucher_num]').val()
        let add_time = $(this).closest('[lll]').find('[add_name=add_time]').val()
        let add_borrowingChoose_id = $(this).closest('[lll]').find('[add_name=add_borrowingChoose]').val()
        let add_borrowingChoose = $(this).closest('[lll]').find('[add_name=add_borrowingChoose] option:selected').text()
        let add_happenMoney = $(this).closest('[lll]').find('[add_name=add_happenMoney]').val()
        let add_abstract = $(this).closest('[lll]').find('[add_name=add_abstract]').val()

        let add_Relation_time = $(this).closest('[lll]').find('[add_name=add_Relation_time]').val()
        let add_Relation_money_name = $(this).closest('[lll]').find('[add_name=add_Relation_money] option:selected').text()
        let add_Relation_money_id = $(this).closest('[lll]').find('[add_name=add_Relation_money] option:selected').attr('data-id')
        let add_Relation_money = $(this).closest('[lll]').find('[add_name=add_Relation_money]').val()
        let add_Relation_sum = $(this).closest('[lll]').find('[add_name=add_Relation_sum]').val()
        let add_file = IMG_arr2

        if(add_voucher_type == '' || add_voucher_num == '' || add_time == '' || add_borrowingChoose == '' || add_abstract == '' || add_Relation_time == '' || add_Relation_money == '' || add_Relation_sum == ''){
            $('#yz_modal').html('您有内容没有填写完整！')
            $('#yz_modal').addClass('active')
            tipShow()
            return
        }
        let that = this
        $.ajax({
            url:'http://jk.com/Admin/CostStaff/addCostStaff',
            type:'post',
            data:{
                vouch_sn:add_voucher_type+'-'+add_voucher_num,
                cost_time:add_time,
                cost_type:add_borrowingChoose_id,
                cost_fmoney:add_happenMoney,
                cost_desc:add_abstract,
                cal_id:add_Relation_money_id,
                cost_tmoney:add_Relation_sum,
                vimgs:add_file
            },
            success:function(data){
                console.log(data)
                if(data.code==1){
                    var peopleTableHTML = $('#table_table').children('tbody').html()
                    peopleTableHTML += `
                                <tr data-index="${data.id}">
                                        <td><input type="checkbox" class="checkboxStyle"></td>
                                        <td data-name="edit_voucher">${add_voucher_type}-${add_voucher_num}</td>
                                        <td data-name="edit_time">${add_time}</td>
                                        <td data-name="edit_borrowingChoose">${add_borrowingChoose}</td>
                                        <td data-name="edit_happenMoney">${add_happenMoney}</td>
                                        <td data-name="edit_Relation_money">${add_Relation_money_name}</td>
                                        <td data-name="edit_Relation_sum">${add_Relation_sum}</td>
                                        <td class="look_record" data-name="edit_abstract">
                                            <a href="#" class="look_record_btn">查看</a>
                                            <div class="tip_hiddenBox work_record_box">
                                                <span class="work_record_title">摘要</span>
                                                <p class="work_record_main">${add_abstract}</p>
                                                <button class="btn btn-sm btn-success">确认</button>
                                            </div>
                                        </td>
                                        <td data-name="edit_file"><a href="look_img" class="edit_table">查看</a></td>
                                        <td style><a href="edit_people" class="edit_table">编辑</a></td>
                                    </tr>
                        `
                    $('#table_table').children('tbody').html(peopleTableHTML)

                    $(that).closest('[lll]').find('[add_name]').val('')
                    $(that).closest('[lll]').removeClass('active')
                    IMG_arr2 = []
                }else{
                    $('#yz_modal').html(data.msg)
                    $('#yz_modal').addClass('active')
                    tipShow()
                    return
                }
            }
        })

    })
    // 确认并新增按钮点击时
    $('#add_AddNew_btn').click(function(){
        let add_voucher_type = $(this).closest('[lll]').find('[add_name=add_voucher_type]').val()
        let add_voucher_num = $(this).closest('[lll]').find('[add_name=add_voucher_num]').val()
        let add_time = $(this).closest('[lll]').find('[add_name=add_time]').val()
        let add_borrowingChoose_id = $(this).closest('[lll]').find('[add_name=add_borrowingChoose]').val()
        let add_borrowingChoose = $(this).closest('[lll]').find('[add_name=add_borrowingChoose] option:selected').text()
        let add_happenMoney = $(this).closest('[lll]').find('[add_name=add_happenMoney]').val()
        let add_abstract = $(this).closest('[lll]').find('[add_name=add_abstract]').val()
        let add_Relation_time = $(this).closest('[lll]').find('[add_name=add_Relation_time]').val()
        let add_Relation_money_name = $(this).closest('[lll]').find('[add_name=add_Relation_money] option:selected').text()
        let add_Relation_money_id = $(this).closest('[lll]').find('[add_name=add_Relation_money] option:selected').attr('data-id')
        let add_Relation_money = $(this).closest('[lll]').find('[add_name=add_Relation_money]').val()
        let add_Relation_sum = $(this).closest('[lll]').find('[add_name=add_Relation_sum]').val()
        let add_file = IMG_arr2

        if(add_voucher_type == '' || add_voucher_num == '' || add_time == '' || add_borrowingChoose == '' || add_abstract == '' || add_Relation_time == '' || add_Relation_money == '' || add_Relation_sum == ''){
            $('#yz_modal').html('您有内容没有填写完整！')
            $('#yz_modal').addClass('active')
            tipShow()
            return
        }
        let that = this
        $.ajax({
            url:'http://jk.com/Admin/CostStaff/addCostStaff',
            type:'post',
            data:{
                vouch_sn:add_voucher_type+'-'+add_voucher_num,
                cost_time:add_time,
                cost_type:add_borrowingChoose_id,
                cost_fmoney:add_happenMoney,
                cost_desc:add_abstract,
                cal_id:add_Relation_money_id,
                cost_tmoney:add_Relation_sum,
                vimgs:add_file
            },
            success:function(data){
                console.log(data)
                if(data.code==1){
                    var peopleTableHTML = $('#table_table').children('tbody').html()
                    peopleTableHTML += `
                                <tr data-index="${data.id}">
                                        <td><input type="checkbox" class="checkboxStyle"></td>
                                        <td data-name="edit_voucher">${add_voucher_type}-${add_voucher_num}</td>
                                        <td data-name="edit_time">${add_time}</td>
                                        <td data-name="edit_borrowingChoose">${add_borrowingChoose}</td>
                                        <td data-name="edit_happenMoney">${add_happenMoney}</td>
                                        <td data-name="edit_Relation_money">${add_Relation_money_name}</td>
                                        <td data-name="edit_Relation_sum">${add_Relation_sum}</td>
                                        <td class="look_record" data-name="edit_abstract">
                                            <a href="#" class="look_record_btn">查看</a>
                                            <div class="tip_hiddenBox work_record_box">
                                                <span class="work_record_title">摘要</span>
                                                <p class="work_record_main">${add_abstract}</p>
                                                <button class="btn btn-sm btn-success">确认</button>
                                            </div>
                                        </td>
                                        <td data-name="edit_file"><a href="look_img" class="edit_table">查看</a></td>
                                        <td style><a href="edit_people" class="edit_table">编辑</a></td>
                                    </tr>
                        `
                    $('#table_table').children('tbody').html(peopleTableHTML)

                    $(that).closest('[lll]').find('[add_name]').val('')
                    IMG_arr2 = []
                }else{
                    $('#yz_modal').html(data.msg)
                    $('#yz_modal').addClass('active')
                    tipShow()
                    return
                }
            }
        })

    })

    /*燃料金额 自动计算*/
    // $('[add_name=edit_num]').blur(function(){
    //     let edit_num = $(this).val();
    //     let edit_price = $(this).closest('[lll]').find('[add_name=edit_price]').val();
    //     if(edit_num&&edit_price){
    //         let add_sum_money = Number(edit_num)*Number(edit_price)
    //         $(this).closest('[lll]').find('[add_name=edit_sum_money]').val(edit_sum_money.toFixed(2))
    //     }else{
    //         $(this).closest('[lll]').find('[add_name=edit_sum_money]').val('')
    //         return
    //     }
    // })
    // $('[add_name=edit_price]').blur(function(){
    //     let add_price = $(this).val();
    //     let add_num = $(this).closest('[lll]').find('[add_name=add_num]').val();
    //     if(add_price&&add_num){
    //         let add_sum_money = Number(add_num)*Number(add_price)
    //         $(this).closest('[lll]').find('[add_name=add_sum_money]').val(add_sum_money.toFixed(2))
    //     }else{
    //         $(this).closest('[lll]').find('[add_name=add_sum_money]').val('')
    //         return
    //     }
    // })

    /*关联费用选择之后自动计算金额*/
    $('[add_name=edit_Relation_money]').change(function(){
        let edit_Relation_money = $(this).val()
        $(this).closest('[lll]').find('[add_name=edit_Relation_sum]').val(edit_Relation_money);
    })
    // 保存按钮点击时
    $('#save_btn').click(function(){
        let edit_voucher_type = $(this).closest('[lll]').find('[add_name=edit_voucher_type]').val()
        let edit_voucher_num = $(this).closest('[lll]').find('[add_name=edit_voucher_num]').val()
        let edit_time = $(this).closest('[lll]').find('[add_name=edit_time]').val()
        let edit_borrowingChoose_id = $(this).closest('[lll]').find('[add_name=edit_borrowingChoose]').val()
        let edit_borrowingChoose = $(this).closest('[lll]').find('[add_name=edit_borrowingChoose] option:selected').text()
        let edit_happenMoney = $(this).closest('[lll]').find('[add_name=edit_happenMoney]').val()
        let edit_abstract = $(this).closest('[lll]').find('[add_name=edit_abstract]').val()

        let edit_Relation_time = $(this).closest('[lll]').find('[add_name=edit_Relation_time]').val()
        let edit_Relation_money_name = $(this).closest('[lll]').find('[add_name=edit_Relation_money] option:selected').text()
        let edit_Relation_money_id = $(this).closest('[lll]').find('[add_name=edit_Relation_money] option:selected').attr('data-id')
        let edit_Relation_money = $(this).closest('[lll]').find('[add_name=edit_Relation_money]').val()
        let edit_Relation_sum = $(this).closest('[lll]').find('[add_name=edit_Relation_sum]').val()

        let edit_id = window.sessionStorage['edit_id']
        let that = this
        let edit_file = JSON.parse(window.sessionStorage['edit_img_box']).imgEditArr2.concat(IMG_arr2)

        if(edit_voucher_type == '' || edit_voucher_num == '' || edit_time == '' || edit_borrowingChoose_id == '' || edit_happenMoney == '' || edit_abstract == '' || edit_Relation_money_id == '' || edit_Relation_sum == ''){
            $('#yz_modal').html('您有内容没有填写完整！')
            $('#yz_modal').addClass('active')
            tipShow()
            return
        }

        $.ajax({
            url:'http://jk.com/Admin/CostStaff/editCostStaff',
            type:'post',
            data:{
                id:edit_id,
                vouch_sn:edit_voucher_type+'-'+edit_voucher_num,
                cost_time:edit_time,
                cost_type:edit_borrowingChoose_id,
                cost_fmoney:edit_happenMoney,
                cost_desc:edit_abstract,
                cal_id:edit_Relation_money_id,
                cost_tmoney:edit_Relation_sum,
                vimgs:edit_file
            },
            success:function(data){
                if(data.code==1){
                    let tableHTML = $('#table_table').find(`tr[data-index=${edit_id}]`)
                    tableHTML.children('[data-name=edit_voucher]').html(edit_voucher_type+'-'+edit_voucher_num)
                    tableHTML.children('[data-name=edit_time]').html(edit_time)
                    tableHTML.children('[data-name=edit_borrowingChoose]').html(edit_borrowingChoose)
                    tableHTML.children('[data-name=edit_happenMoney]').html(edit_happenMoney)
                    tableHTML.children('[data-name=edit_abstract]').html(edit_abstract)
                    tableHTML.children('[data-name=edit_Relation_money_name]').html(edit_Relation_money_name)
                    tableHTML.children('[data-name=edit_Relation_sum]').html(edit_Relation_sum)
                    $(that).parent().parent().removeClass('active')
                    window.sessionStorage['edit_id'] = ''
                    let edit_img_box = {
                        imgEditArr2:[]
                    }

                    window.sessionStorage['edit_img_box'] = JSON.stringify(edit_img_box)
                }
            }
        })
    })
    // 取消按钮点击时
    $('.button_list').on('click','button',function(){
        if($(this).hasClass('cancel_btn')){
            $(this).closest('[lll]').removeClass('active')
        }
    })


    // 弹出来的小框框
    function tipShow(){
        window.tipBox = setTimeout(function(){
            $('#yz_modal').removeClass('active')
            clearTimeout(tipBox)
        },2000)
    }
    //关闭所有已选择的框框
    function checkBox(){
        var checkboxs =$('input[type=checkbox]');
        for (var i = 0; i < checkboxs.length; i++) {
            if (!checkboxs[i].checked) {
                continue;
            }else{
                $(checkboxs[i]).attr('checked',false)
            }
        }
    }

})